<template><div><h2 id="快速入门" tabindex="-1"><a class="header-anchor" href="#快速入门"><span>快速入门</span></a></h2>
<p>Laravel 6 中文文档 /</p>
<p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="测试-入门指南" tabindex="-1"><a class="header-anchor" href="#测试-入门指南"><span>测试：入门指南</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#environment">环境</a></li>
<li><a href="#creating-and-running-tests">创建并运行测试</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p><code v-pre>Laravel</code> 天生就具有测试的基因。事实上，<code v-pre>Laravel</code> 默认就支持用 <code v-pre>PHPUnit</code> 来做测试，并为你的应用程序配置好了 <code v-pre>phpunit.xml</code> 文件。框架还提供了一些便利的辅助函数，让你可以更直观的测试你的应用程序。</p>
<p>默认情况，你的应用 <code v-pre>tests</code> 目录中包含两个子目录：<code v-pre>Feature</code> 和 <code v-pre>Unit</code>。单元测试是针对你的代码中非常少，而且相对独立的一部分代码来进行的测试。实际上，大部分单元测试都是针对单个方法进行的。功能测试是针对大面积代码进行的测试，包括多个对象之间的交互，甚至是对 <code v-pre>JSON</code> 端点的完整 <code v-pre>HTTP</code> 请求。</p>
<p><code v-pre>Feature</code> 和 <code v-pre>Unit</code> 目录中都提供一个 <code v-pre>ExampleTest.php</code> 测试示例文件。安装一个新的 <code v-pre>Laravel</code> 应用程序之后，在命令行下运行 <code v-pre>phpunit</code> 命令，即可运行测试。</p>
<h2 id="环境" tabindex="-1"><a class="header-anchor" href="#环境"><span>环境</span></a></h2>
<p>在使用 <code v-pre>phpunit</code> 进行测试时，<code v-pre>Laravel</code> 将根据 <code v-pre>phpunit.xml</code> 文件设定的环境变量自动将环境设置为 <code v-pre>testing</code>，并将 <code v-pre>Session</code> 及缓存以 <code v-pre>array</code> 的形式存储，也就是说在测试时不会持久化任何 <code v-pre>Session</code> 或缓存数据。</p>
<p>你可以随意创建其它必要的测试环境配置。<code v-pre>testing</code> 环境变量可以在 <code v-pre>phpunit.xml</code> 文件中修改，但是在运行测试之前，请确保使用 <code v-pre>config:clear</code> Artisan 命令来清除配置信息的缓存！</p>
<p>此外，你还可以在你的项目根目录下创建一个 <code v-pre>.env.testing</code> 文件，在运行单元测试，或者使用带有 <code v-pre>--env=testing</code> 选项的 Artisan 命令时，<code v-pre>.env</code> 文件中的变量会被这个文件覆盖。</p>
<h2 id="创建并运行测试" tabindex="-1"><a class="header-anchor" href="#创建并运行测试"><span>创建并运行测试</span></a></h2>
<p>可以使用 Artisan 命令 <code v-pre>make:test</code> 创建一个新的测试用例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 在 Feature 目录下创建一个测试类...</span></span>
<span class="line">php artisan make<span class="token punctuation">:</span>test UserTest</span>
<span class="line"></span>
<span class="line"><span class="token comment">// 在 Unit 目录下创建一个测试类...</span></span>
<span class="line">php artisan make<span class="token punctuation">:</span>test UserTest <span class="token operator">--</span>unit</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>测试类生成之后，你就可以像平常使用 PHPUnit 一样来定义测试方法。要运行测试只需要在终端上运行 <code v-pre>phpunit</code> 命令即可：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Unit</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>TestCase</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>RefreshDatabase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">TestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 一个基本的测试示例</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">testBasicTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">assertTrue</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：如果要在你的测试类中定义自己的 <code v-pre>setUp</code> / <code v-pre>tearDown</code> 方法，请确保调用了父类中的 <code v-pre>parent::setUp()</code> / <code v-pre>parent::tearDown()</code> 方法。</p>
</blockquote>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/testing/5182" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/tes...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/testing/5182" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/tes...</a></p>
</blockquote>
</div></template>


